package com.erp.service.app;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;


/**
 * NDApp
 */
public class NDApp extends Application {


    //刚启动程序
    public static boolean isStart = true;

    //程序上下文
    public static Context context;

    //是否是服务
    public static boolean isService;

    //用户登录信息Key
    public static String USERLOGINSP_KEY = "userInfo";


    //线程池
    public static ExecutorService threadPool = Executors.newCachedThreadPool();

    //机子唯一标识
    private static String deviceUUID = null;

    //机子唯一标识SHA散列
    private static String deviceUUIDSHA = null;

    //91U传过来的sid
    public static String sid = "";

    //91U传过来的uid
    public static long uid = 0;
    //91U传过来的uname
    public static String uname = null;
    //91U传过来的工号
    public static String workid = null;
    //91U传过来的单位ID
    public static long unitid;
    //91U传过来的部门ID
    public static long departmentid;
    //是否已经91U验证
    public static boolean isIMRegister = true;


    //	@Override
//	public void onCreate() {
//        super.onCreate();
//	}

    public static void uid(long id){
        uid = id;
    }


    public void fakeonCreate(Context c, String erp, String server, String oldServer){
        if(TextUtils.isEmpty(erp)){
            erp = "http://ioa.99.com";
        }
        if(TextUtils.isEmpty(server)){
            server = "http://mobile.ioa.99.com/ServiceHost";
        }else if( !server.endsWith("ServiceHost")){
            server += "/ServiceHost";
        }
        if(TextUtils.isEmpty(oldServer)){
            oldServer = "http://ioa.99.com/Ajax";
        }

        SysContext.erpUrl = erp;//"http://ioa.99.com";
        SysContext.serverUrl = server;//"http://mobile.ioa.99.com/ServiceHost";
        SysContext.oldServerUrl = oldServer;//"http://ioa.99.com/Ajax";
        context = c;

}

    /**
     * 应用程序初始化
     */
    public static void init() {
        USERLOGINSP_KEY = "userInfo";
        SysContext.initSysContext();
    }

    /**
     * 获取机子唯一标识
     */
    public static String getDeviceUUID() {
        if (deviceUUID == null) {
            TelephonyManager tm = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
            deviceUUID = tm.getDeviceId();
            if(TextUtils.isEmpty(deviceUUID))
                deviceUUID="";
        }

        return deviceUUID;
    }

    /**
     * 获取手机型号
     */
    public static String getPhoneModel(){
        return Build.MODEL;
    }

    /**
     * 获取SharedPreferences的键�
     *
     * @author wqz
     *
     * @param key
     *            {@link String} 关键�
     * @param defaultValue
     *            {@link String} 默认�
     * @return
     */
    public static String getShareConfig(String key, String defaultValue) {
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(context);
        return prefs.getString(key, defaultValue);
    }

    /**
     * 获取SharedPreferences的键�
     *
     * @author wqz
     *
     * @param key
     *            {@link String} 关键�
     * @param defaultValue
     *            {@link String} 默认�
     * @return
     */
    public static boolean getShareConfig(String key, boolean defaultValue) {
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(context);
        return prefs.getBoolean(key, defaultValue);
    }

    /**
     * 根据Text查找Array.xml的Value�
     *
     * @author wqz
     *
     * @param text
     *            {@link String} Text�
     * @param textArrResID
     *            {@link Integer} Text数组的资�
     * @param valueArrResID
     *            {@link Integer} Value数组的资�
     * @return
     */
    public static String getXMLArrayValueByText(String text, int textArrResID,
                                                int valueArrResID) {
        String value = "";
        String[] textArr = context.getResources().getStringArray(textArrResID);
        String[] valueArr = context.getResources()
                .getStringArray(valueArrResID);

        for (int i = 0; i < textArr.length; i++) {
            if (textArr[i].equals(text)) {
                value = valueArr[i];
                break;
            }
        }

        return value;
    }

    /**
     * 根据Value查找Array.xml的Text
     *
     * @author wqz
     *
     * @param value
     *            {@link String} Value
     * @param textArrResID
     *            {@link Integer} Text数组的资源
     * @param valueArrResID
     *            {@link Integer} Value数组的资源
     * @return
     */
    public static String getXMLArrayTextByValue(String value, int textArrResID,
                                                int valueArrResID) {
        String text = "";
        String[] textArr = context.getResources().getStringArray(textArrResID);
        String[] valueArr = context.getResources()
                .getStringArray(valueArrResID);

        for (int i = 0; i < valueArr.length; i++) {
            if (valueArr[i].equals(value)) {
                text = textArr[i];
                break;
            }
        }
        return text;
    }

}
